package com.pengxun.manager.controller.system;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.enums.UserTypeEnums;
import com.pengxun.manager.model.system.Company;
import com.pengxun.manager.model.system.UserRole;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.CompanyService;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.UserRoleService;
import com.pengxun.manager.service.system.UserinfoService;
import com.pengxun.manager.vo.CompanyVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/company")
public class CompanyController {
    private static final Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @Resource
    private CompanyService companyService;
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private UserRoleService userRoleService;

    @Resource
    private LoginService loginService;

    @RequestMapping("/insert")
    public Result insert(@RequestBody Company company) {
        try {
            companyService.insert(company);
            //新建公司管理员账户
            Userinfo user = new Userinfo();
            user.setCompanyId(company.getCompanyId());
            user.setAccount(company.getEmail());
            user.setPassword("Aa1234");
            user.setEmail(company.getEmail());
            user.setUserType(UserTypeEnums.COMPANY_ADMIN.getType());
            userinfoService.createUser(user);
            
            //设置租户管理员初始化角色
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getUserId());
            userRole.setRoleCode("roleCode000008");
            userRoleService.insert(userRole);
            
            //发送设置密码邮件
            userinfoService.sendSetPasswordEmail(company.getEmail());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Company company) {
        try {
            companyService.update(company);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/delete")
    public Result delete(Integer companyId) {
        try {
            companyService.delete(companyId);
        } catch (EastrocException e) {
            return ResultUtils.renderError(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody CompanyVO companyVO) {
        try {
            return ResultUtils.render(companyService.findByPage(companyVO));
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/findByCompanyId")
    public Result findById(Integer companyId) {
        try {
            Company company = companyService.findByCompanyId(companyId);
            return ResultUtils.render(company);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }


    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            return ResultUtils.render(companyService.findAll());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultUtils.renderError(e.getMessage());
        }
    }


    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody Company company) {
        try {
            companyService.updateStatus(company);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }


    @RequestMapping("/getCompanyStructure")
    public Result getCompanyStructure() {
        try {
            return ResultUtils.render(companyService.getCompanyStructure());
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/getCurrentUserCompany")
    public Result getCompanyType(){
        try {
            Userinfo currentUser = loginService.getCurrentUser();
            Integer companyId = currentUser.getCompanyId();
            Company company = companyService.findByCompanyId(companyId);
            return ResultUtils.render(company);
        }catch (Exception e){
            return  ResultUtils.renderError(e.getMessage());
        }
    }

}
